<?php
/**
 * Created by PhpStorm.
 * User: fenfen
 * Date: 2017/1/3
 * Time: 17:51
 */

namespace bc\model;

use bc\ar\MActiveRecord;
use bc\model\ReporttitleModel;
use bc\model\UserreportModel;
use bc\util\Util;
class ReportModel extends MActiveRecord
{
    public static function tableName()
    {
        return '{{%user_report}}';
    }
    
    /**
     * 
 	 * 根据用户user_id和bind_no获取用户的报告信息
 	 * 
   */
    public function getReportInfoTemp($user_id,$bind_no,$product_id)
    {
        if(empty($user_id)|| empty($bind_no) || empty($bind_no) ){
            return array();
        }

        //获取标题数据
        $ReporttitleM = new ReporttitleModel();
        $ReporttitleData = $ReporttitleM->getInfoFromProductId($product_id);
        //获取
        $UserreportM = new UserreportModel();
        $UserreportData = $UserreportM->getInfoFromBindno($product_id,$bind_no);
        //获取用户基本信息
        $ItemM = new ItemModel();
        $ItemD = $ItemM->getUserInfoByUseridAndProductId($user_id,$product_id,$bind_no);
        foreach($ItemD as $k => $value){
            if($value['biitem_type']==2 || $value['biitem_type']==3 || $value['biitem_type']==4){
                if(empty($data[$value['report_key']])){
                    if($value['bsitem_type'] == 1) {
                        $data[$value['report_key']] = $value['subitem_value'];
                    }else{
                        $data[$value['report_key']] = $value['subitem_name'];
                    }
                }else {
                    if($value['bsitem_type'] == 1){
                        $data[$value['report_key']] = $data[$value['report_key']] . ";" . $value['subitem_value'];
                    }else{
                        $data[$value['report_key']] = $data[$value['report_key']] . ";" . $value['subitem_name'];
                    }

                }

            }else{
                $data[$value['report_key']]=$value['item_value'];
            }
        }

        //判断产品为2时
        if($product_id ==2 ) {
            //计算BMI=体重/身高*身高
            if ($data['info_height'] == 0 || $data['info_weight'] == 0) {
                $data['info_bmi'] = "数值错误";
            } else {
                $data['info_bmi'] = round($data['info_weight'] / (($data['info_height'] / 100) * ($data['info_height'] / 100)), 1);
            }
        }elseif($product_id ==3){
            //计算年龄
            $age=Util::birthday($data['info_birthday']);
            $data['age'] = $age;
            $vdreferenceM=new VdreferenceModel();
//            var_dump($data);
            //获取VD的推荐投入量
            $vdData=$vdreferenceM->getReference($UserreportData['report_1_value'],$data['info_period'],1);
            $refVD=$vdreferenceM->getRefData($age,$vdData);
            $data['refVD'] = $refVD;

            //获取盖的推荐投入量
            $gaiData=$vdreferenceM->getReference($UserreportData['report_1_value'],$data['info_period'],2);
            $refGAI=$vdreferenceM->getRefData($age,$gaiData);
            $data['refCA'] = $refGAI;
        }else{

        }

        $data = array_merge($data, $UserreportData);
        $data = array_merge($data, $ReporttitleData);

        return  $data;
    }
    
    
    
    /**
     * 开启事务方式执行插入数据
     * @param array $reportData
     * @param array $reportInfo
     */
    public function tractionInsert($reportData, $reportInfo)
    {
    	$transaction = $this->getDb()->beginTransaction();
    	try 
    	{
	    	foreach ($reportData as $key=>$value)
	    	{
	    		$sql = 'INSERT ' . self::getTableSchema ()->name . "(product_id, bind_no, report_key, report_value, user_id, deleted) VALUES ( {$reportInfo->product_id}, '{$reportInfo->bind_no}', '{$key}', '{$value}', '{$reportInfo->user_id}', 0)";
	    		$this->getDb()->createCommand($sql)->execute();
	    	}
    		
    		$transaction->commit();
    	}
		catch (\Exception $e) 
    	{
			$transaction->rollBack();
			throw $e;
    	}
	}
    	
}